package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.vo.ChannelVO;
import com.cskaoyan.bean.admin.vo.OrderDetailVO;
import com.cskaoyan.bean.admin.vo.OrderRefundVO;
import com.cskaoyan.bean.admin.vo.OrderReqVO;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.utils.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Jcy
 * 订单管理
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    @RequiresPermissions("admin:order:list")
    public BaseRespVo list(OrderReqVO orderReq) {
        BasePageData data = orderService.list(orderReq);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("channel")
    public BaseRespVo channel() {
        List<ChannelVO> channelVOS = new ArrayList<>();
//        0: {code: "ZTO", name: "中通快递"}
//        1: {code: "YTO", name: "圆通速递"}
//        2: {code: "YD", name: "韵达速递"}
//        3: {code: "YZPY", name: "邮政快递包裹"}
//        4: {code: "EMS", name: "EMS"}
//        5: {code: "DBL", name: "德邦快递"}
//        6: {code: "FAST", name: "快捷快递"}
//        7: {code: "ZJS", name: "宅急送"}
//        8: {code: "TNT", name: "TNT快递"}
//        9: {code: "UPS", name: "UPS"}
//        10: {code: "DHL", name: "DHL"}
//        11: {code: "FEDEX", name: "FEDEX联邦(国内件)"}
//        12: {code: "FEDEX_GJ", name: "FEDEX联邦(国际件)"}
        channelVOS.add(new ChannelVO("ZTO", "中通快递"));
        channelVOS.add(new ChannelVO("YTO", "圆通速递"));
        channelVOS.add(new ChannelVO("YD", "韵达速递"));
        channelVOS.add(new ChannelVO("YZPY", "邮政快递包裹"));
        channelVOS.add(new ChannelVO("EMS", "EMS"));
        channelVOS.add(new ChannelVO("DBL", "德邦快递"));
        channelVOS.add(new ChannelVO("FAST", "快捷快递"));
        channelVOS.add(new ChannelVO("ZJS", "宅急送"));
        channelVOS.add(new ChannelVO("TNT", "TNT快递"));
        channelVOS.add(new ChannelVO("UPS", "UPS"));
        channelVOS.add(new ChannelVO( "DHL", "DHL"));
        channelVOS.add(new ChannelVO( "FEDEX", "FEDEX联邦(国内件)"));

        return BaseRespVo.ok(channelVOS);
    }

    @RequestMapping("detail")
    @RequiresPermissions("admin:order:read")
    public BaseRespVo detail(Integer id) {
        OrderDetailVO detailVO = orderService.detail(id);
        return BaseRespVo.ok(detailVO);
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:order:delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = ((Integer) map.get("orderId"));
        int code = orderService.delete(orderId);
        if (code == Constant.FAIL) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    @RequiresPermissions("admin:order:refund")
    public BaseRespVo refund(@RequestBody OrderRefundVO orderRefundVO) {
        Integer orderId = orderRefundVO.getOrderId();
        Double refundMoney = orderRefundVO.getRefundMoney();
        orderService.refund(orderId, BigDecimal.valueOf(refundMoney));
        return BaseRespVo.ok();
    }

    @RequestMapping("ship")
    @RequiresPermissions("admin:order:ship")
    public BaseRespVo ship(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        orderService.ship(orderId,shipChannel,shipSn);
        return BaseRespVo.ok();
    }
}
